/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.hanfu.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.result.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.continew.admin.hanfu.domain.bo.SampleBo;
import top.continew.admin.hanfu.domain.dto.SampleDto;
import top.continew.admin.hanfu.domain.query.SampleQuery;
import top.continew.admin.hanfu.domain.vo.SampleVo;
import top.continew.admin.hanfu.domain.vo.wx.SampleDeWxVo;
import top.continew.admin.hanfu.domain.vo.wx.SampleWxVo;
import top.continew.admin.hanfu.service.impl.SampleServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xieling
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/sample")
@RequiredArgsConstructor
public class SampleController {

    private final SampleServiceImpl sampleService;

    /**
     * 根据条件查询
     */
    @PostMapping("/condition")
    public R<Page<SampleVo>> getSampleListByCondition(@RequestBody SampleQuery sampleQuery) {

        Page<SampleVo> sampleListByCondition = sampleService.getSampleListByCondition(sampleQuery);

        /**
         * 格式化时间格式
         */
        sampleListByCondition.getRecords().forEach(item -> {
            item.setUpdateTime(DateTime.of(Long.parseLong(item.getUpdateTime())).toString("yyyy-MM-dd HH:mm:ss"));
        });

        return R.success(sampleListByCondition);

    }

    /**
     * 新增样片接口
     */
    @PostMapping("add")
    public R addSampleService(@RequestBody SampleDto sampleDto) {

        sampleDto.setUpdateTime(String.valueOf(System.currentTimeMillis()));

        int id = sampleService.addSample(sampleDto);

        return id != -1 ? R.success(id) : R.fail("添加失败");
    }

    /**
     * 修改样片图片
     */
    @PostMapping("/img")
    public R updateSampleImg(@RequestParam("img") List<MultipartFile> img,
                             @RequestParam("imageId") String newOssId,
                             @RequestParam("sampleId") Integer id,
                             @RequestParam("user") String user) {

        SampleBo sampleBo = new SampleBo();

        sampleBo.setImage(img);
        sampleBo.setImageIds(newOssId);
        sampleBo.setId(id);
        sampleBo.setUploader(user);

        int i = sampleService.updateSampleImg(sampleBo);

        return i == 1 ? R.success() : R.fail("更新失败");
    }

    /**
     * 根据id获得样片
     */
    @GetMapping("/{id}")
    public R<SampleVo> getSampleById(@PathVariable("id") Integer id) {

        SampleVo byId = sampleService.getSampleById(id);

        return R.success(byId);
    }

    /**
     * 更新样片
     */
    @PostMapping("up")
    public R updateSample(@RequestBody SampleDto sampleDto) {

        int id = sampleService.updateSample(sampleDto);

        return R.success(id);
    }

    /**
     * 删除样片
     */
    @DeleteMapping("")
    public R deleteSample(@RequestBody ArrayList<Integer> ids) {

        boolean b = sampleService.removeByIds(ids);

        return b ? R.success() : R.fail("");
    }

    /**
     * 微信端获取样片数据
     * 
     * @param sampleQuery
     * @return
     */
    @SaIgnore
    @PostMapping("/wx/condition")
    public R<Page<SampleWxVo>> getSampleListByConditionWx(@RequestBody SampleQuery sampleQuery) {

        Page<SampleWxVo> sampleListByConditionWx = sampleService.getSampleListByConditionWx(sampleQuery);

        return R.success(sampleListByConditionWx);
    }

    @SaIgnore
    @GetMapping("/wx/{id}")
    public R<SampleDeWxVo> getSampleDeWxVoById(@PathVariable("id") Integer id) {
        SampleDeWxVo sampleDeWxVoById = sampleService.getSampleDeWxVoById(id);

        return R.success(sampleDeWxVoById);
    }

}
